<template>
  <div class="layui-container fly-marginTop">
    <div class="fly-panel fly-panel-user" pad20>
      <div class="layui-tab layui-tab-brief" lay-filter="user">
        <ul class="layui-tab-title">
          <li>
            <router-link :to="{name:'login'}">登入</router-link>
          </li>
          <li class="layui-this">注册</li>
        </ul>
        <ValidationObserver ref="observer" v-slot="{validate}">
          <div class="layui-form layui-tab-content" id="LAY_ucm" style="padding: 20px 0;">
            <div class="layui-tab-item layui-show">
              <div class="layui-form layui-form-pane">
                <form method="post">
                  <div class="layui-form-item">
                    <ValidationProvider name="username" rules="required|email" v-slot="{errors}">
                      <div class="layui-row">
                        <label class="layui-form-label">用户名</label>
                        <div class="layui-input-inline">
                          <input
                            type="text"
                            name="username"
                            v-model.trim="username"
                            placeholder="请输入标题"
                            autocomplete="off"
                            class="layui-input"
                          />
                        </div>
                        <div class="layui-form-mid layui-word-aux">将会成为您唯一的登入名</div>
                      </div>

                      <div class="form-error">{{errors[0]}}</div>
                    </ValidationProvider>
                  </div>
                  <div class="layui-form-item">
                    <label class="layui-form-label">昵称</label>
                    <ValidationProvider name="name" rules="required" v-slot="{errors}">
                      <div class="layui-input-inline">
                        <input
                          type="text"
                          name="name"
                          v-model="name"
                          placeholder="请输入昵称"
                          autocomplete="off"
                          class="layui-input"
                        />
                      </div>
                      <div class="layui-form-mid form-error">{{errors[0]}}</div>
                    </ValidationProvider>
                  </div>
                  <div class="layui-form-item">
                    <label class="layui-form-label">密码</label>
                    <ValidationProvider rules="required|min:6" vid="password" v-slot="{errors}">
                      <div class="layui-input-inline">
                        <input
                          type="password"
                          name="password"
                          v-model="password"
                          placeholder="请输入密码"
                          autocomplete="off"
                          class="layui-input"
                        />
                      </div>
                      <div class="layui-form-mid form-error">{{errors[0]}}</div>
                    </ValidationProvider>
                  </div>
                  <div class="layui-form-item">
                    <label class="layui-form-label">确认密码</label>
                    <ValidationProvider rules="confirmed:password" v-slot="{errors}">
                      <div class="layui-input-inline">
                        <input
                          type="password"
                          name="checkpass"
                          v-model="checkpass"
                          placeholder="请再次输入密码"
                          autocomplete="off"
                          class="layui-input"
                        />
                      </div>
                      <div class="layui-form-mid form-error">{{errors[0]}}</div>
                    </ValidationProvider>
                  </div>
                  <div class="layui-form-item">
                    <label class="layui-form-label">验证码</label>
                    <ValidationProvider name="code" rules="required|length:4" v-slot="{errors}">
                      <div class="layui-input-inline">
                        <input
                          type="text"
                          name="code"
                          v-model="code"
                          placeholder="请输入标题"
                          autocomplete="off"
                          class="layui-input"
                        />
                      </div>
                      <span class="svg" @click="getCaptcha()" v-html="svg"></span>
                      <div class="form-error">{{errors[0]}}</div>
                    </ValidationProvider>
                  </div>
                  <div class="layui-form-item">
                    <button class="layui-btn" type='button' @click="validate().then(reg)">立即注册</button>
                  </div>
                  <div class="layui-form-item fly-form-app">
                    <span>或者直接使用社交账号快捷注册</span>
                    <a
                      href
                      onclick="layer.msg('正在通过QQ登入', {icon:16, shade: 0.1, time:0})"
                      class="iconfont icon-qq"
                      title="QQ登入"
                    ></a>
                    <a
                      href
                      onclick="layer.msg('正在通过微博登入', {icon:16, shade: 0.1, time:0})"
                      class="iconfont icon-weibo"
                      title="微博登入"
                    ></a>
                  </div>
                </form>
              </div>
            </div>
          </div>
        </ValidationObserver>

      </div>
    </div>
  </div>
</template>

<script>
import { getCode, reg } from '@/api/login.js'
import { ValidationProvider, ValidationObserver } from 'vee-validate'

export default {
  name: 'reg',
  data () {
    return {
      svg: '',
      password: '',
      username: '',
      name: '',
      checkpass: '',
      code: ''
    }
  },
  components: {
    ValidationProvider,
    ValidationObserver
  },
  async mounted () {
    this.getCaptcha()
  },
  methods: {
    async reg () {
      const valid = await this.$refs.observer.validate()
      if (valid) {
        const regInfo = {
          username: this.username,
          password: this.password,
          code: this.code,
          name: this.name,
          sid: this.$store.state.sid
        }
        const res = await reg(regInfo)
        if (res.code === 200) {
          this.username = ''
          this.password = ''
          this.checkpass = ''
          this.code = ''
          this.name = ''
          requestAnimationFrame(() => {
            this.$refs.observer.reset()
          })
          this.$alert('注册成功')
          setTimeout(() => {
            this.$router.push('/login')
          })
        } else {
          this.$refs.observer.setErrors(res.msg)
        }
      }
    },
    async getCaptcha () {
      const sid = this.$store.state.sid
      const res = await getCode(sid)

      if (res.code === 200) {
        this.svg = res.data
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.svg {
  position: relative;
  top: -10px;
}
.form-error {
  color: red;
}
</style>
